import { ref, reactive, computed } from 'vue'
import { defineStore } from 'pinia'
import { doLoginAPI } from '@/api/user'

export const useAuthStore = defineStore('auth', () => {
  const auth = reactive({
    userid: null,
    token: null,
  })
  const isLogin = computed(() => {
    return !!(auth.userid && auth.token)
  })
  const exitAndUpdateAuth = () => {
    auth.userid = null
    auth.token = null
  }
  const loginAndUpdateAuth = async (data: LoginParamsRaw) => {
    try {
      let result = await doLoginAPI(data)
      console.log(result);
      let { userid, token } = result.data
      auth.userid = userid
      auth.token = token
    } catch (err: any) {
      // throw new Error(err.message)
      return Promise.reject(err)
    }
    // await doLoginAPI(data)
  }
  return { auth, isLogin, loginAndUpdateAuth }
  // const count = ref(0)
  // const doubleCount = computed(() => count.value * 2)
  // function increment() {
  //   count.value++
  // }

  // return { count, doubleCount, increment }
}, {
  persist: true,
  // persist:{
  //   // key:"lw-auth",
  //   // paths: ['auth.token'],
  // },
})
